package com.jx.dao;

import com.jx.entity.Classes;
import com.jx.entity.Student;
import com.jx.mapper.ClassesMapper;
import com.jx.mapper.StudentMapper;
import com.jx.util.MybatisUtil;
import com.jx.util.PageResult;
import com.jx.vo.StudentVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentDao {

	// 1. 查询所有学生
	public List<Student> findAll() throws IOException {
		try (SqlSession session = MybatisUtil.getSqlSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			List<Student> students = mapper.findAll();
			for (Student student : students) {
				// 取出学生所在的班级编号
				Integer cid = student.getCid();
				// 根据班级编号查询出班级对象
				Classes classes = classesMapper.findById(cid);
				// 为学生设置班级名称
				student.setCname(classes.getCname());
			}
			return students;
		}
	}
	// 2. 分页查询
	public PageResult<Student> findByPage(int page, int pageSize) throws IOException {
		// 2.1 开始分页
		PageHelper.startPage(page,pageSize);
		// 2.2 查询得到所有学生信息
		List<Student> students = findAll();
		// 2.3 将查询结果转换为Page<Student>对象
		Page< Student> pageStudents = (Page<Student>) students;
		// 2.4 最后返回构造的PageResult对象
		return new PageResult<>(pageStudents.getTotal(),pageStudents.getResult());

	}
	// 条件查询带分页
	public PageResult<Student> search(int page, int pageSize, StudentVo studentVo) throws IOException {
		// 1. 开始分页
		PageHelper.startPage(page,pageSize);
		// 2. 开始分页查询
		try(SqlSession session = MybatisUtil.getSqlSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			List<Student> students = mapper.search(studentVo);
			for (Student student : students) {
				// 取出学生所在的班级编号
				Integer cid = student.getCid();
				// 根据班级编号查询出班级对象
				Classes classes = classesMapper.findById(cid);
				// 为学生设置班级名称
				student.setCname(classes.getCname());
			}
			// 2.3 将查询结果转换为Page<Student>对象
			Page< Student> pageStudents = (Page<Student>) students;
			// 2.4 最后返回构造的PageResult对象
			return new PageResult<>(pageStudents.getTotal(),pageStudents.getResult());
		}
	}
	// 3. 添加学生
	public void add(Student student) throws IOException {
		try(SqlSession session = MybatisUtil.getSqlSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.add(student);
		}
	}

	// 4. 修改学生
	public void update(Student student) throws IOException {
		try(SqlSession session = MybatisUtil.getSqlSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.update(student);
		}
	}

	public void delete(String[] split) throws IOException {
		try(SqlSession session = MybatisUtil.getSqlSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			for (String s : split) {
				mapper.delete(s);
			}
		}
	}
}
